const express = require('express');
const router = express.Router();
const businessAnalyticsService = require('../../services/businessAnalyticsService');
const { authenticateToken } = require('../../middleware/auth');
const { adminAuth } = require('../../middleware/adminAuth');
const { checkPermission } = require('../../middleware/permission');

/**
 * 获取积分经济分析
 */
router.get('/points-economy', authenticateToken, adminAuth, checkPermission('analytics:view'), async (req, res) => {
  try {
    const { startDate, endDate } = req.query;
    
    if (!startDate || !endDate) {
      return res.status(400).json({
        success: false,
        message: '请提供开始日期和结束日期'
      });
    }

    const analysis = await businessAnalyticsService.getPointsEconomyAnalysis(startDate, endDate);
    
    res.json({
      success: true,
      data: analysis
    });
  } catch (error) {
    console.error('获取积分经济分析失败:', error);
    res.status(500).json({
      success: false,
      message: error.message || '获取积分经济分析失败'
    });
  }
});

/**
 * 获取广告效果分析
 */
router.get('/ad-effect', authenticateToken, adminAuth, checkPermission('analytics:view'), async (req, res) => {
  try {
    const { startDate, endDate } = req.query;
    
    if (!startDate || !endDate) {
      return res.status(400).json({
        success: false,
        message: '请提供开始日期和结束日期'
      });
    }

    const analysis = await businessAnalyticsService.getAdEffectAnalysis(startDate, endDate);
    
    res.json({
      success: true,
      data: analysis
    });
  } catch (error) {
    console.error('获取广告效果分析失败:', error);
    res.status(500).json({
      success: false,
      message: error.message || '获取广告效果分析失败'
    });
  }
});

/**
 * 获取商品销售分析
 */
router.get('/product-sales', authenticateToken, adminAuth, checkPermission('analytics:view'), async (req, res) => {
  try {
    const { startDate, endDate } = req.query;
    
    if (!startDate || !endDate) {
      return res.status(400).json({
        success: false,
        message: '请提供开始日期和结束日期'
      });
    }

    const analysis = await businessAnalyticsService.getProductSalesAnalysis(startDate, endDate);
    
    res.json({
      success: true,
      data: analysis
    });
  } catch (error) {
    console.error('获取商品销售分析失败:', error);
    res.status(500).json({
      success: false,
      message: error.message || '获取商品销售分析失败'
    });
  }
});

/**
 * 获取财务分析
 */
router.get('/financial', authenticateToken, adminAuth, checkPermission('analytics:view'), async (req, res) => {
  try {
    const { startDate, endDate } = req.query;
    
    if (!startDate || !endDate) {
      return res.status(400).json({
        success: false,
        message: '请提供开始日期和结束日期'
      });
    }

    const analysis = await businessAnalyticsService.getFinancialAnalysis(startDate, endDate);
    
    res.json({
      success: true,
      data: analysis
    });
  } catch (error) {
    console.error('获取财务分析失败:', error);
    res.status(500).json({
      success: false,
      message: error.message || '获取财务分析失败'
    });
  }
});

/**
 * 获取综合业务分析报告
 */
router.get('/comprehensive', authenticateToken, adminAuth, checkPermission('analytics:view'), async (req, res) => {
  try {
    const { startDate, endDate } = req.query;
    
    if (!startDate || !endDate) {
      return res.status(400).json({
        success: false,
        message: '请提供开始日期和结束日期'
      });
    }

    const analysis = await businessAnalyticsService.getComprehensiveAnalysis(startDate, endDate);
    
    res.json({
      success: true,
      data: analysis
    });
  } catch (error) {
    console.error('获取综合业务分析失败:', error);
    res.status(500).json({
      success: false,
      message: error.message || '获取综合业务分析失败'
    });
  }
});

module.exports = router;